//
//  PublicAlert.m
//  AlertAction
//
//  Created by Seven-Augus on 2017/2/27.
//  Copyright © 2017年 Seven-Augus. All rights reserved.
//

#import "PublicAlert.h"

@interface PublicAlert ()

@end

@implementation PublicAlert

+ (void)publicSimpleAlertController:(UIViewController *)VC Title:(NSString *)title Message:(NSString *)message Style:(UIAlertControllerStyle)preferredStyle CancelActionTitle:(NSString *)cancelActionTitle SureActionTitle:(NSString *)sureActionTitle DeleteActionTitle:(NSString *)deleteActionTitle CancelActions:(void (^)(__autoreleasing id *))cancelActions SureActions:(void (^)(__autoreleasing id *))sureActions DeleteActions:(void (^)(__autoreleasing id *))deleteActions
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:deleteActionTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        deleteActions(nil);
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelActionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        cancelActions(nil);
    }];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sureActionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        sureActions(nil);
    }];
    
    if (!(cancelActionTitle.length == 0)) {
        [alertController addAction:cancelAction];
    }
    if (!(sureActionTitle.length == 0)) {
        [alertController addAction:sureAction];
    }
    if (!(deleteActionTitle.length == 0)) {
        [alertController addAction:deleteAction];
    }
    
    [VC presentViewController:alertController animated:YES completion:nil];
}

+ (void)publicWithTextFiledAlertConroller:(UIViewController *)VC Title:(NSString *)title Message:(NSString *)message Style:(UIAlertControllerStyle)preferredStyle TextFileFirstPlace:(NSString *)textFileFirstPlace TextFileSecondPlace:(NSString *)textFileSecondPlace CancelActionTitle:(NSString *)cancelActionTitle SureActionTitle:(NSString *)sureActionTitle CancelActions:(void (^)(__autoreleasing id *))cancelActions SureActions:(void (^)(__autoreleasing id *))sureActions
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = textFileFirstPlace;
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = textFileSecondPlace;
        textField.secureTextEntry = YES;
        
//        [[NSNotificationCenter defaultCenter] addObserver:VC selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    }];
    
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:cancelActionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        cancelActions(nil);
    }];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sureActionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        sureActions(nil);
    }];
    
    [alertController addAction:cancleAction];
    [alertController addAction:sureAction];
    
    [VC presentViewController:alertController animated:YES completion:nil];
}

//- (void)alertTextFieldDidChange:(NSNotification *)notification{
//    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
//    if (alertController) {
//        UITextField *login = alertController.textFields.firstObject;
//        UIAlertAction *sureAction = alertController.actions.lastObject;
//        sureAction.enabled = login.text.length > 2; // 密码框输入三个以上字符，确定按钮才能点击
//    }
//}

@end
